package org.example;

import org.example.gen.CallBack;
import org.example.tool.ShapeTool;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class FileDirTool {
    private  List<String> bxgsmcs;
    private  List<String> zwmcs;
    private  List<String> nfs;
    private  List<String> qhdms;
    private  List<String> provinceCodes;
    private  List<String> cityCodes;
    private  List<String> countyCodes;
    private  List<String> townCodes;
    private LogInterface log;
    private String shapePath;
    private String currentDirectory;
    private Level level;
    public FileDirTool(String shape,String outputPath, LogInterface  log ) {
        this.shapePath = shape;
        this.log = log;
        currentDirectory = outputPath+File.separator;

    }

    public  boolean createDir(){
        bxgsmcs= new ArrayList<>(ShapeTool.getDistinctFields(shapePath,"bxgsmc"));
        zwmcs= new ArrayList<>(ShapeTool.getDistinctFields(shapePath,"zwmc"));
        nfs= new ArrayList<>(ShapeTool.getDistinctFields(shapePath,"nf"));
        qhdms= new ArrayList<>(ShapeTool.getDistinctFields(shapePath,"qhdm"));
        HashSet<String> townCodes=new HashSet<>();
        HashSet<String> countyCodes=new HashSet<>();
        HashSet<String> cityCodes=new HashSet<>();
        HashSet<String> provinceCodes=new HashSet<>();
        for (String qhdm : qhdms) {
            townCodes.add(qhdm.substring(0, 9));
            countyCodes.add(qhdm.substring(0, 6));
            cityCodes.add(qhdm.substring(0, 4));
            provinceCodes.add(qhdm.substring(0, 4));
        }
        if(provinceCodes.size()>1){
            log.logError("多个省级代码");
            return false;
        }
        if(qhdms.size()==1){
            // 同一个村的数据
            level= Level.VILLAGE;
            for (String qhdm : qhdms){
                createRootDir(qhdm);
            }
        }
        else if(townCodes.size()==1){
            //同一个乡镇下的数据
            level= Level.TOWN;
            for (String townCode : townCodes){
                createRootDir(townCode);
            }
        }else if(countyCodes.size()==1){
            //同一个县下的数据
            level= Level.COUNTY;
            for (String countyCode : countyCodes){
                createRootDir(countyCode);
            }

        }else if(cityCodes.size()==1){
            // 同一个市下的数据
            level= Level.CITY;
            for (String cityCode : cityCodes){
                createRootDir(cityCode);
            }

        }else{
            // 同的省下的数据
            level= Level.PROVINCE;
            for (String provinceCode : provinceCodes){
                createRootDir(provinceCode);
            }

        }
        this.provinceCodes=new ArrayList<>(provinceCodes);
        this.cityCodes=new ArrayList<>(cityCodes);
        this.countyCodes=new ArrayList<>(countyCodes);
        this.townCodes=new ArrayList<>(townCodes);

        return true;
    }

    /**
     * 根据数据遍历数据
     * @param callBack 回调
     * @return
     */
    public boolean traverse(CallBack callBack, int startProgress, int progressBarMax){
        boolean  result=true;
        int total=nfs.size()*bxgsmcs.size()*zwmcs.size()*qhdms.size();
        int current=0;
        int progressBarStep=(progressBarMax-startProgress)/total;
        for (String nf : nfs){
            for (String bxgsmc : bxgsmcs){
                for (String zwmc : zwmcs){
                    String baseDir;
                    switch ( level){
                        case PROVINCE:
                            baseDir= currentDirectory +  bxgsmc+  provinceCodes.get(0) + nf+  zwmc;
                            for(String qhdm:qhdms){
                                String villageDir= baseDir+File.separator+qhdm.substring(0, 4) +File.separator+qhdm.substring(0, 6) +File.separator+qhdm.substring(0, 9)+ File.separator + qhdm;
                                createDir(villageDir);
                                //输出进度
                                result=result&&callBack.doWork(nf, bxgsmc, qhdm, zwmc, villageDir);
                                current++;
                                log.updateProgress(startProgress+current*progressBarStep);
                            }
                            break;
                        case CITY:
                            baseDir= currentDirectory +  bxgsmc+ cityCodes.get(0) +  nf+ zwmc;
                            for(String qhdm:qhdms){
                                String villageDir= baseDir +File.separator+qhdm.substring(0, 6) +File.separator+qhdm.substring(0, 9)+ File.separator + qhdm;
                                createDir(villageDir);
                                result=result&&callBack.doWork(nf, bxgsmc, qhdm, zwmc, villageDir);
                                current++;
                                log.updateProgress(startProgress+current*progressBarStep);
                            }
                            break;
                        case COUNTY:
                            baseDir= currentDirectory +  bxgsmc+  countyCodes.get(0) +  nf+ zwmc;
                            for(String qhdm:qhdms){
                                String villageDir= baseDir +File.separator+qhdm.substring(0, 9)+ File.separator + qhdm;
                                createDir(villageDir);
                                result=result&&callBack.doWork(nf, bxgsmc, qhdm, zwmc, villageDir);
                                current++;
                                log.updateProgress(startProgress+current*progressBarStep);
                            }
                            break;
                        case TOWN:
                            baseDir= currentDirectory +  bxgsmc+  townCodes.get(0) +  nf+  zwmc;
                            for(String qhdm:qhdms){
                                String villageDir= baseDir + File.separator + qhdm;
                                createDir(villageDir);
                                result=result&&callBack.doWork(nf, bxgsmc, qhdm, zwmc, villageDir);
                                current++;
                                log.updateProgress(startProgress+current*progressBarStep);
                            }
                            break;
                        case VILLAGE:
                            baseDir= currentDirectory +  bxgsmc+  qhdms.get(0) + nf+  zwmc;
                            result=result&&callBack.doWork(nf, bxgsmc, qhdms.get(0), zwmc, baseDir);
                            current++;
                            log.updateProgress(startProgress+current*progressBarStep);
                            break;
                        default:break;
                    }
                }
            }
        }
        return result;
    }



    private void createDir(String path){
        java.io.File file = new java.io.File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }
    private  void createRootDir(String qhdm){
        for (String nf:nfs){
            for(String bxgsmc:bxgsmcs){
                for (String zwmc:zwmcs){
                    String dirName = currentDirectory +  bxgsmc+  qhdm +  nf+  zwmc;
                    File file = new File(dirName);
                    if (!file.exists()) {
                        file.mkdirs();
                        log.logInfo("创建目录: " + dirName);
                    }
                }
            }
        }
    }



    // 创建一个枚举，代表不同等级
    public enum Level {
        PROVINCE,
        CITY,
        COUNTY,
        TOWN,
        VILLAGE
    }
}
